#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then create some           
#   new files, do an Incremental and restore those two files.
# test the bvfs interface
#
TestName="bvfs-cmds-test"
JobName=Incremental 
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
echo "${tmpsrc}" >${tmp}/file-list

mkdir -p $tmpsrc
cp -p $src/src/dird/*.c $tmpsrc

change_jobname CompressedTest $JobName
start_test

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "ClientRunAfterJob", "sleep 2", "Job")'

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/dird.c"
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/ua_cmds.c"
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/ua_dotcmds.c"
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

cat <<EOF >$tmp/bconcmds
@$out /dev/null
use MyCatalog
@$out $tmp/log0.out
sql
SELECT JobId, StartTime, EndTime FROM Job;

EOF

run_bconsole

start=`perl -ne '/(\d)\s+\|\s+([0-9 :-]+?)\s*\|/ && ($1 == 2) && print $2' $tmp/log0.out` 

cat <<EOF >$tmp/bconcmds
@$out /dev/null
use MyCatalog
@$out $tmp/log3.out
.bvfs_get_jobids client=$CLIENT
@$out $tmp/log4.out
.bvfs_get_jobids client=$CLIENT count
@$out $tmp/log5.out
.bvfs_get_jobids client=$CLIENT count limit=2
@$out $tmp/log6.out
.bvfs_get_jobids client=$CLIENT count start="$start"
@$out $tmp/log7.out
.bvfs_get_jobids client=$CLIENT count end="$start"
EOF

run_bconsole

estat=0
grep '^1,2,3,4$' $tmp/log3.out  >/dev/null # All jobids
estat=`expr $estat + $?`
grep '^4$' $tmp/log4.out        >/dev/null # Number of jobs
estat=`expr $estat + $?`
grep '^2$' $tmp/log5.out        >/dev/null # with a limit
estat=`expr $estat + $?`
grep '^3$' $tmp/log6.out        >/dev/null # with a filter on the date
estat=`expr $estat + $?`
grep '^1$' $tmp/log7.out        >/dev/null # with a filter on the date
estat=`expr $estat + $?`

stop_bacula
touch $tmp/log2.out
check_two_logs

end_test
